<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
$file = $_FILES['attach'];
if($file['name'] == '')
{
    echo "<script>alert('请选择要上传的文件！');history.back();</script>";
	return;
}

/*---------文件类型检查-------------*/
$validtypes = "gif|jpg|jpeg|bmp|png|zip|rar|exe";

$pathparts = pathinfo($file['name']);
$suffix = $pathparts['extension'];

$bValidType = false;
$types = explode("|",$validtypes);
foreach ($types as $type)
{
	if($type == $suffix) $bValidType = true;
}
if(!$bValidType)
{
	echo "<script>alert('文件类型不正确，只允许上传".$validtypes."格式的文件！');history.back();</script>";
	return;
}
/*----------------------------------*/

$dest_dir = "../attach";
$dest_file = $dest_dir.'/'.date("ymd").'_'.$file['name'];
if(is_uploaded_file($file['tmp_name']))
{
    $ret = move_uploaded_file($file['tmp_name'],$dest_file);
}
if($ret)
{
    echo "<script>alert('上传文件成功！');</script>";
}
else
{
	switch($file['error'])
	{
		case 1:
		case 2:
			echo "<script>alert('上传文件失败,文件过大');history.back();</script>";
			break;
		default:
			echo "<script>alert('上传文件失败: ".$file['error']." ，请检查文件是否合法！');history.back();</script>";
			break;
	}
	return;
}
chmod($dest_file,0755);

echo "<div><span align='center'>文件上传为： ";
echo "<a href='".$dest_file."'>".basename($dest_file)."</a></span>    ";
echo "<span align='center'>文件大小： ".filesize($dest_file)."</span></div>";
?>
